Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Collections;
20:
21: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
22: import org.openrdf.model.Resource;
23: import org.openrdf.model.Statement;
24: import org.openrdf.model.URI;
25: import org.openrdf.model.ValueFactory;
26:
27: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
28: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
29: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
30: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
31: import cz.cvut.kbss.ontodriver.model.Axiom;
32: import cz.cvut.kbss.ontodriver.model.NamedResource;
33:
34: class ReferencedListIterator extends AbstractSesameIterator {
35:
36:         private final ReferencedListDescriptor listDescriptor;
37:
38:         private final URI hasContentProperty;
39:
40:         private URI currentProperty;
41:
42:         private Statement currentNode;
43:         private Statement currentContent;
44:         private Collection<Statement> next;
45:
46:         public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector,
47:                         ValueFactory vf) throws SesameDriverException {
48:                 super(listDescriptor, connector, vf);
49:                 this.listDescriptor = listDescriptor;
50:                 this.hasContentProperty = SesameUtils.toSesameUri(listDescriptor.getNodeContent()
51:                                                                                                                                                 .getIdentifier(), vf);
52:                 this.currentProperty = hasListProperty;
53:                 init();
54:         }
55:
56:         private void init() throws SesameDriverException {
57:                 this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred,
58:                                 context);
59:         }
60:
61:         @Override
62:         public boolean hasNext() throws SesameDriverException {
63:                 return (!next.isEmpty());
64:         }
65:
66:         @Override
67:         public Resource nextNode() throws SesameDriverException {
68:                 nextInternal();
69:                 return (Resource) currentNode.getObject();
70:         }
71:
72:         private void nextInternal() throws SesameDriverException {
73:                 if (!hasNext()) {
74:                         throw new IllegalStateException();
75:                 }
76:                 checkSuccessorMax(next, currentProperty);
77:                 this.currentNode = next.iterator().next();
78:                 this.currentProperty = currentNode.getPredicate();
79:                 checkNodeIsResource(currentNode);
80:                 final Resource elem = (Resource) currentNode.getObject();
81:                 this.currentContent = getNodeContent(elem);
82:                 this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
83:         }
84:
85:         private Statement getNodeContent(Resource node) throws SesameDriverException {
86:                 final Collection<Statement> elems = connector.findStatements(node, hasContentProperty,
87:                                 null, includeInferred, context);
88:                 checkSuccessorMax(elems, hasContentProperty);
89:                 if (elems.isEmpty()) {
90:                         throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
91:                 }
92:                 final Statement elem = elems.iterator().next();
93:                 checkNodeIsResource(elem);
94:                 return elem;
95:         }
96:
97:         @Override
98:         public Resource currentContent() throws SesameDriverException {
99:                 assert currentContent.getObject() instanceof Resource;
100:                 return (Resource) currentContent.getObject();
101:         }
102:
103:         @Override
104:         public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
105:                 nextInternal();
106:                 assert currentContent.getObject() instanceof Resource;
107:
108:                 return createAxiom(currentContent.getSubject(), listDescriptor.getNodeContent(),
109:                                 (Resource) currentContent.getObject());
110:         }
111:
112:         @Override
113:         public void remove() throws SesameDriverException {
114:                 assert currentNode.getObject() instanceof Resource;
115:                 final Collection<Statement> toRemove = new ArrayList<>();
116:                 toRemove.add(currentNode);
117:                 toRemove.add(currentContent);
118:                 if (!next.isEmpty()) {
119:                         toRemove.addAll(next);
120:                         final Statement stmt = next.iterator().next();
121:                         checkNodeIsResource(stmt);
122:                         final Resource nextNode = (Resource) stmt.getObject();
123:                         final Statement connectNext = vf.createStatement(currentNode.getSubject(),
124:                                         currentProperty, nextNode, context);
125:                         this.next = Collections.singleton(connectNext);
126:
127:                         this.currentNode = null;
128:                         this.currentContent = null;
129:                         connector.addStatements(next);
130:                 } else {
131:                         next = Collections.emptyList();
132:                 }
133:                 connector.removeStatements(toRemove);
134:         }
135:
136:         @Override
137:         public void replaceCurrentWith(NamedResource newContent) throws SesameDriverException {
138:                 assert currentNode.getObject() instanceof Resource;
139:                 // We just replace the original content statement with new one
140:                 connector.removeStatements(Collections.singleton(currentContent));
141:                 final Resource node = (Resource) currentNode.getObject();
142:                 final Statement stmt = vf.createStatement(node, hasContentProperty,
143:                                 SesameUtils.toSesameUri(newContent.getIdentifier(), vf), context);
144:                 connector.addStatements(Collections.singleton(stmt));
145:         }
146:
147: }